#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=2010;
int n,k,num[maxn],total[maxn];
struct person
{
	int time;
	int father;
	person():time(0){}
}P[maxn];
map<string,int> mp,rec;
map<int,string> rmp;
 
int findFather(int x)
{
	int a=x;
	while(x!=P[x].father)
		x=P[x].father;
	while(a!=P[a].father)
	{
		int temp=a;
		a=P[a].father;
		P[temp].father=x;
	}
	return x;
}
void Union(int a,int b)
{
	int a1=findFather(a);
	int b1=findFather(b);
	if(a1!=b1)
	{
		if(P[a1].time<P[b1].time)
			P[a1].father=b1;
		else
			P[b1].father=a1;
	}
}
int main()
{
	scanf("%d %d",&n,&k);
	string a,b;
	for(int i=0;i<maxn;++i)
		P[i].father=i;
	int t,no=0;
	vector<pair<int,int> > ope;
	for(int i=0;i<n;++i)
	{
		cin>>a>>b>>t;
		if(mp.count(a)==0)
		{
			rmp[no]=a;
			mp[a]=no++;
		}
		if(mp.count(b)==0)
		{
			rmp[no]=b;
			mp[b]=no++;
		}
		P[mp[a]].time+=t;
		P[mp[b]].time+=t;
		ope.push_back(make_pair(mp[a],mp[b]));
	}
	for(int i=0;i<ope.size();++i)
	{
		Union(ope[i].first,ope[i].second);
	}
	memset(num,0,sizeof(num));
	memset(total,0,sizeof(total));
	for(int i=0;i<no;++i)
	{
		int temp=findFather(i);
		++num[temp];
		total[temp]+=P[i].time;
	}
	for(int i=0;i<no;++i)
	{
		if(num[i]>2&&total[i]/2>k)
			rec[rmp[i]]=num[i];
	}
	printf("%d\n",rec.size());
	for(map<string,int > ::iterator it=rec.begin();it!=rec.end();++it)
	{
		printf("%s %d\n",it->first.c_str(),it->second);
	}
    return 0;
}